import Vue from 'vue'
import Vuex from 'vuex'
import cart from './cart/index.js'
import user from './user/index.js'
Vue.use(Vuex)
const STORAGE_KEY = 'search-list'
const store = new Vuex.Store({
	state:{
		searchList:uni.getStorageSync(STORAGE_KEY) || []
	},
	getters:{},
	mutations:{
		searchval(state,data){
			//新输入的值存在的话
			if(state.searchList.indexOf(data) !=-1){
				state.searchList.splice(data,1)
				state.searchList.unshift(data)
				// 存到本地
				uni.setStorage({
					key:STORAGE_KEY,
					data:state.searchList
				})
			}else{
				state.searchList.unshift(data)
				//存到本地
				uni.setStorage({
					key:STORAGE_KEY,
					data:state.searchList
				})
			}
			//当搜索历史值的长度有10个了，就删掉最后一个
			if(state.searchList.length > 10){
				state.searchList.splice(10,state.searchList.length -10 )
			}
		},
		//清空搜索历史数据
		haodoqingkong(state){
			state.searchList = []
			//把已置空的数组重新赋值给本地，相当于清空了本地数据
			uni.setStorage({
				key:STORAGE_KEY,
				data:state.searchList
			})
		}
	},
	actions:{},
	modules:{cart,user}
})

export default store